<?php

namespace app\api\model;

use think\Model;

class WechatCard extends ApiModel
{
    // 表名
    protected $visible = ['card_id','logo_url','brand_name','title','sub_title',
        'notice','description','date_type','begin_timestamp','end_timestamp',
        'fixed_term','fixed_begin_term','quantity','createtime','status',
        'discount','least_cost','reduce_cost','gift','deal_detail','default_detail'];

    protected function base($query)
    {
        $mpId = get_api_applet_id();
        $globalKey = [
            'mp_id'=>$mpId,
            'visible'=>1,
        ];
        $query->where($globalKey);
    }

    //根据商品Id 获取评论
    public static function getList($map = [] ,$page = 1, $row = 10)
    {
        $res['data'] = self::where($map)
            ->page($page, $row)
            ->order('createtime desc')
            ->select();
        $res['total'] = $total = self::where($map)->count();
        $res['has_more'] = ceil($total / $row) > $page ? true : false;
        return $res;
    }

    //根据商品Id 获取评论
    public static function getListByUser($userId ,$page = 1, $row = 10)
    {
        $map = ['user_id'=>$userId];
        $res['data'] = WechatCardCode::where($map)
            ->with('info')
            ->page($page, $row)
            ->order('state desc,begin_time asc')
            ->select();
        $res['total'] = $total = WechatCardCode::where($map)->count();
        $res['has_more'] = ceil($total / $row) > $page ? true : false;
        return $res;
    }
}
